/*
 * j4Win
 *  http://code.google.com/p/j4win/
 *
 * Copyright (C) 2006-2007 Felix Geyer, Moritz Laschinger
 *
 * This file is part of j4Win.
 *
 * j4Win is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * j4Win is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package j4win;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Scoreboard {
	private ImageObserver observer;
	private Toolkit toolkit;
	private BufferedImage i;
	private Graphics g;
	private FontMetrics fm;
	
	private Image sbImage;
	
	public static final int SCOREHEIGHT = 300;
	public static final int SCOREWIDTH = 250;
	
	public Scoreboard(Toolkit toolkit, ImageObserver observer) {
		this.observer = observer;
		this.toolkit = toolkit;
		
		i = new BufferedImage(
			SCOREWIDTH, 
			SCOREHEIGHT, 
			BufferedImage.TYPE_INT_ARGB
		);
		g = i.getGraphics();
		fm = g.getFontMetrics();
		
		sbImage = toolkit.getImage(getClass().getClassLoader().getResource("gfx/scoreboardlarge.png"));
		ImageTracker.addImage(sbImage);
	}
	
	public BufferedImage drawScoreboard(Field field, int playerTurn, int winner) {
		int currHeight = 30;
		
		g.setColor(MainFrame.backgroundColor);
		g.fillRect(0, 0, i.getWidth(), i.getHeight());
		g.drawImage(sbImage, 0, 0, observer); //$NON-NLS-1$
		
		g.setColor(new Color(200, 200, 200));
		
		// draw IP
		g.setFont(new Font("Verdana", 0, 14));
		try {
			g.drawString("IP: " + InetAddress.getLocalHost().getHostAddress(), 20, 280);
		} catch (UnknownHostException e) {
		}
		
		if (playerTurn == 0) {
			return i;
		}
		
		int[] stonesLeft  = {0, 0};
		if (field != null) {
			stonesLeft[0] = field.getStonesLeft(1);
			stonesLeft[1] = field.getStonesLeft(2);
		}
		
		// Player draw
		g.setFont(new Font("Verdana", Font.BOLD, 16)); //$NON-NLS-1$
		
		if (winner==-1)
			g.drawString(Messages.getString("Scoreboard.player") + " " + playerTurn + Messages.getString("Scoreboard.turn"), 15, currHeight); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		else if (winner==3)
			g.drawString(Messages.getString("Scoreboard.tie"), 15, currHeight); //$NON-NLS-1$
		else
			g.drawString(Messages.getString("Scoreboard.player") + " " + winner + Messages.getString("Scoreboard.won"), 15, currHeight); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		
		currHeight += fm.getHeight()+8;
		
		// Player 1
		g.drawImage(toolkit.getImage(getClass().getClassLoader().getResource("gfx/cellred.png")), 15, currHeight-fm.getHeight()+3, observer);
		g.setFont(new Font("Verdana", Font.BOLD, 16)); //$NON-NLS-1$
		g.drawString(Messages.getString("Scoreboard.player")+" 1:", 35, currHeight); //$NON-NLS-1$ //$NON-NLS-2$
		currHeight += fm.getHeight()+2;
		
		// Number of available coins
		g.setFont(new Font("Verdana", 0, 14)); //$NON-NLS-1$
		g.drawString(Messages.getString("Scoreboard.coins") + stonesLeft[0], 35, currHeight); //$NON-NLS-1$
		currHeight += fm.getHeight()+8;
		
		
		// Player 2
		g.drawImage(toolkit.getImage(getClass().getClassLoader().getResource("gfx/cellgreen.png")), 15, currHeight-fm.getHeight()+3, observer);
		g.setFont(new Font("Verdana", Font.BOLD, 16)); //$NON-NLS-1$
		g.drawString(Messages.getString("Scoreboard.player")+ " 2:", 35, currHeight); //$NON-NLS-1$ //$NON-NLS-2$
		currHeight += fm.getHeight()+2;
		
		// Number of available coins
		g.setFont(new Font("Verdana", 0, 14)); //$NON-NLS-1$
		g.drawString(Messages.getString("Scoreboard.coins") + stonesLeft[1], 35, currHeight); //$NON-NLS-1$
		currHeight += fm.getHeight()+2;
		
		return i;
	}
}